package ui;
//pachet login
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.ImageIcon;

import model.Users;

import controller.AuthentificationController;
import controller.ControllerException;

import javax.swing.JPasswordField;

import ui.administrator.Administrator;
import ui.asistent.Asistent;
import util.Application;
import util.Constants;

@SuppressWarnings({ "serial", "unused" })
public class Login extends JFrame implements ActionListener{
	

	private JPanel contentPane;
	private JTextField textField;
	
	private AuthentificationController controller;
	private JPasswordField passwordField;

	

	public void setController(AuthentificationController controller) {
		this.controller = controller;
	}

	

	public Login() {
		setBackground(new Color(47, 79, 79));
		setFont(new Font("Dialog", Font.BOLD, 12));
		setTitle("Login");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 439, 243);
		contentPane = new JPanel();
		contentPane.setForeground(new Color(0, 0, 0));
		contentPane.setBackground(new Color(240, 255, 240));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(199, 53, 203, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblUsername = new JLabel("Utilizator");
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblUsername.setBounds(115, 51, 74, 20);
		contentPane.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Parola");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblPassword.setBounds(115, 110, 74, 14);
		contentPane.add(lblPassword);
		
		JButton btnLogin = new JButton("Autentificare");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				autentif();
			}
		});
		
			
		btnLogin.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnLogin.setBounds(115, 148, 287, 40);
		contentPane.add(btnLogin);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(Login.class.getResource("/assets/images/login.png")));
		lblNewLabel.setBounds(10, 39, 146, 109);
		contentPane.add(lblNewLabel);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(199, 104, 203, 20);
		contentPane.add(passwordField);
	}




	public void autentif() {
		try {
			System.out.println("Login.autentif()");
			Users user=	controller.login(textField.getText(), new String(passwordField.getPassword()));
			if(user.getRol()== Constants.Rol.ADMINISTRATOR){
				Administrator frame= (Administrator) Application.context.getBean("Administrator");
				frame.setVisible(true);
			} else
			if(user.getRol() == Constants.Rol.ASISTENT){
				Asistent frame = (Asistent) Application.context.getBean("Asistent");
				frame.setVisible(true);
			}
			
			} catch (ControllerException e1) {
			
				JOptionPane.showMessageDialog(null, e1.getMessage(),"Atentie",JOptionPane.WARNING_MESSAGE);
			}
		
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}



	
}
